package com.rpframework.website.device.act.api;

import java.util.TimeZone;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.utils.NumberUtils;
import com.rpframework.website.device.dao.IPraiseDao;
import com.rpframework.website.device.domain.Praise;
import com.rpframework.website.device.domain.Stream;
import com.rpframework.website.device.domain.Unit;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.PraiseService;
import com.rpframework.website.device.service.StreamService;
import com.rpframework.website.device.service.UnitService;
import com.rpframework.website.device.service.UserService;

/**
 * 点赞
 * @date 2016年3月14日 下午3:49:38
 */
@Controller
@RequestMapping("/api/praise")
public class PraiseApiAct extends BaseAct{
	@Resource PraiseService service;
	@Resource UnitService unitService;
	@Resource StreamService StreamService;
	@Resource UserService userService;
	public @Resource IPraiseDao iDao;
	/**
	 * 点赞 取消赞
	 * @param relativeId
	 * @param type
	 * @param callback
	 * @param session
	 * @return
	 * @time 2016年3月14日 下午4:06:01
	 */
	@RequestMapping(value="/add" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String listwineds(
			//@RequestParam(value="pager", required=false)Pager<Praise> pager,
			@RequestParam(value="relativeId", required=false)Integer relativeId,//关联表id
			@RequestParam(value="type", required=false)Integer type,//关联类别
			@RequestParam(value="status", required=false)Integer status,//  2收藏   3点赞
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
//		if(pager==null){
//			pager=new Pager<Praise>();
//		}
		User user = getSessionUser(session);
		if(user == null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		JsonObject json = new JsonObject();
		if(!NumberUtils.isValid(relativeId) || !NumberUtils.isValid(type)||!NumberUtils.isValid(status)){
			throw new APICodeException(-5, "参数错误!");
		}
//		Integer i = service.isPraiseByUser(relativeId, type, user.getId());
//		if(i>0){
//			json.addProperty("isPraise", i);//1可以点赞 2不可点赞	
//		}
		Integer size = iDao.queryIntegral(user.getId(),relativeId,status);//查询我是否可以评论，获取积分
		//如果小于1则没有评论过    评论加积分
		if(size<1){
			//增加积分
			Unit unit = unitService.select(1);//评论一条增加积分数
			Integer userUnit = user.getPoint();//获取当前用户积分
			userUnit+=unit.getSC();//增加积分
			user.setPoint(userUnit);//修改积分
			userService.update(user);//提交数据
			//添加积分增加流水
			Stream Stream = new Stream();
			if(status==2)Stream.setTitle("收藏 ");
			else Stream.setTitle("点赞 ");
			Stream.setCreateTime(System.currentTimeMillis()/1000);
			Stream.setUserId(user.getId());
			Stream.setInfo("+"+unit.getSC());
			StreamService.insert(Stream);
		}
		
		
		
		boolean f = service.PraiseByUser(relativeId, type, user.getId(),status);
		Praise t = iDao.doPraiseByUser(relativeId,type,user.getId(),status);
		if(f) {
			json.addProperty("isPraise", t.getState());//1可以点赞 2不可点赞	
			json.addProperty("code", 200);
			json.addProperty("size", size);
			json.addProperty("msg", "操作成功");
		}else{
			json.addProperty("code", 500);
			json.addProperty("msg", "操作失败");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
}
